# coding=utf-8
#该代码示例适用于Python3
import urllib
import urllib.request
import hashlib
import time


def md5(str):
    import hashlib
    m = hashlib.md5()
    m.update(str.encode("utf8"))
    return m.hexdigest()

statusStr = {
    '0': '短信发送成功',
    '-1': '参数不全',
    '-2': '服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间',
    '30': '密码错误',
    '40': '账号不存在',
    '41': '余额不足',
    '42': '账户已过期',
    '43': 'IP地址限制',
    '50': '内容含有敏感词',
    '51': '手机号码不正确'
}

def task(dingxiang_info):
    smsapi = "http://api.smsbao.com/"
    # 短信平台账号
    user = 'your account'
    # 短信平台密码
    password = md5('your password')
    # 要发送的短信内容
    content = '【短信签名】' + dingxiang_info
    print(content)
    # 要发送短信的手机号码，多个号码使用列表
    phone = '139xxxxxxxx'
    if content:
        data = urllib.parse.urlencode({'u': user, 'p': password, 'm': phone, 'c': content})
        send_url = smsapi + 'sms?' + data
        response = urllib.request.urlopen(send_url)
        the_page = response.read().decode('utf-8')
        send_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
        message = send_time + '--' + statusStr[the_page]
        print(message)
        with open('log.log', 'a', encoding='utf-8') as f:
            f.write(message)
            f.write('\n')

# if __name__ == '__main__':
#     task()